#!/usr/bin/python
# -*- coding: utf-8 -*-


'''
参数  类型  描述                                                                              是否必须
tex     String  合成的文本，使用UTF-8编码，请注意文本长度必须小于1024字节                           是
lang    String  语言选择,填写zh                                                                       是
ctp     String  客户端类型选择，web端填写1                                                         是
cuid    String  用户唯一标识，用来区分用户，填写机器 MAC 地址或 IMEI 码，长度为60以内               否
spd     String  语速，取值0-9，默认为5中语速                                                        否
pit     String  音调，取值0-9，默认为5中语调                                                        否
vol     String  语速，取值0-15，默认为5中语速                                                       否
per     String  发音人选择, 0为女声，1为男声，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女   否
'''

from aip import AipSpeech
import time


# This secret key belong to Shengda.Huang
APP_ID = "9418206"
API_KEY = "OIXhZZBzwvSVhOyUsMgfpKxP"
SECRET_KEY = "0f21e1d56df8f0b6bebf570c0a34faff"


def baidu_api(speechstr, filename):
    aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    result = aipSpeech.synthesis(speechstr, 'zh', 1, {
                                'spd': 4,
                                'pit': 6,
                                'vol': 15,
                                'per': 0,
                                })

    if not isinstance(result, dict):
        with open(filename, 'wb') as f:
            f.write(result)
        print('generate %s success from baidu' % filename)
        return 0
    else :
        print(result)
        print('generate %s failed from baidu' % filename)
        return -1


def Test1():
    for i in range(101, 200):
        v_str = "%d" % i
        ret = baidu_api(v_str, v_str + ".mp3")
        print("baidu_api %s ret %d" % (v_str, ret))


def Test2():
    baidu_api("区", "area.mp3")


def Test3():
    for i in range(0, 9):
        c = chr(ord('A') + i)
        s_str = c + '区'
        ret = baidu_api(s_str, c + ".mp3")
        # ret = 0
        print("baidu_api %s ret %d" % (s_str, ret))


if __name__ == '__main__':
    baidu_api("今天几号？", "date.mp3")
